﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;

namespace VisualWebGuiApplication2
{
    public partial class report5 : System.Web.UI.Page
    {
        int tankid = 0;
        string size = string.Empty; 
        string Companyname = string.Empty;
        string TankNumber = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (null != Request.QueryString["id"])
                {
                    tankid = Convert.ToInt32(Request.QueryString["id"].ToString());                   
                   // renderrep(tankid, string.Empty, string.Empty, string.Empty);
                }
                if (null != Request.QueryString["company"])
                {
                    Companyname = Request.QueryString["company"].ToString();
                    // renderrep(tankid, string.Empty, string.Empty, string.Empty);
                }
                if (null != Request.QueryString["tanknumber"])
                {
                    TankNumber = Request.QueryString["tanknumber"].ToString();
                    // renderrep(tankid, string.Empty, string.Empty, string.Empty);
                }
                if (null != Request.QueryString["size"])
                {
                    size = Request.QueryString["size"].ToString();
                    // renderrep(tankid, string.Empty, string.Empty, string.Empty);
                }
                                   
                    renderrep(tankid, size, Companyname, string.Empty);
                
            }
        }

        public void renderrep(int tankid)
        {
            using (GazOnDevEntities db =new GazOnDevEntities())
            {

                string tankstatus = tankid.ToString();
                var query1 = from i in db.Tank_Stocks
                             where i.TankSystemStatus == tankstatus
                             select i;
              

               

                //  dd.FirstOrDefault().Datetime = Convert.ToDateTime(dd.FirstOrDefault().Datetime.Value.ToString("MM/dd/yyyy"));
                //dd.FirstOrDefault().Date_of_Fix = Convert.ToDateTime(dd.FirstOrDefault().Date_of_Fix.Value.ToString("MM/dd/yyyy"));

               List<Tank_Stocks> ds2 = query1.ToList<Tank_Stocks>();
               List<Tank_Stocks> ds3 = new List<Tank_Stocks>();
               if (tankstatus == "3" || tankstatus == "4" || tankstatus == "1")
               {
                   foreach (var item in ds2)
                   {
                       string tankTitle = item.TankNumber + "  מספר שיפוץ  " + item.TankFixNumber + "";
                      
                       if ( tankstatus == "4" )
               {
                           
		 
	                         Invoice inv =  db.Invoices.FirstOrDefault(x=>x.tankNumber == item.TankNumber);
                             if (inv != null)
                             {
                                 string tankTitle2 = item.Address_Tank_From + "\n -------------------- יעד " + inv.address + "";
                               
                                 item.Address_Tank_From = tankTitle2;
                             }

                             
               }

                       if (tankstatus == "4" || tankstatus == "1")
                       {
                           TankFixOperation fixx = db.TankFixOperations.FirstOrDefault(x => x.tankID == item.Id && x.title == "ביצוע בדיקת אטימות");
                           if (fixx != null)
                           {
                               string datett = ((DateTime)fixx.date).ToString("dd/MM/yyyy");
                               string tankTitle3 = item.Datetime + "\n ------------- תאריך שיפוץ " + datett + "";
                               item.Datetime = tankTitle3;
                           }
                       }

                       item.TankNumber = tankTitle;
                       ds3.Add(item);
                   }
                   ds2 = ds3;
               }
               
                
              //  int personSigned = Convert.ToInt32(query1.FirstOrDefault().PersonRecipetsignid);
                // int transportCompanyid = Convert.ToInt32(query1.FirstOrDefault().TransportCompanyID);
                 this.reportViewer1.LocalReport.DataSources.Clear();
                // this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dd));
                 this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds2));
              //     SignPerson personrecipet=   db.SignPersons.FirstOrDefault(x=>x.id == personSigned);
                //  TransportCompany companytrans=   db.TransportCompanies.FirstOrDefault(x=>x.id == transportCompanyid);
                // ReportDataSource rds = new ReportDataSource("DataSet1", dd);
               //  this.reportViewer1.LocalReport.DataSources = dd;
                 
                

                 string tanktype =string.Empty;
                 if (tankstatus == "1")
                {
                tanktype ="במלאי";
                }
                 if (tankstatus == "2")
                {
                    tanktype = "בהמתנה";
                }
                 if (tankstatus == "3")
                {
                    tanktype = "בשיפוץ";
                }
                 if (tankstatus == "4")
                {
                    tanktype = "שיצאות בתעודת משלוח";
                    
 

                }
                 if (tankstatus == "5")
                {
                    tanktype = "פסולים";
                }
                 if (tankstatus == "6")
                {
                    tanktype = "מושמדים";
                }
               
               
              
                 ReportParameter p1 = new ReportParameter("TankType", tanktype);
               
       
             
                reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1});
          
                this.reportViewer1.DataBind();
                reportViewer1.LocalReport.Refresh();
              //  this.reportViewer1.Reset();

           //     this.reportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
                 //ReportDataSource rds = new ReportDataSource("DataSet1",AllFamilies.GetAllProjects);
                 //this.reportViewer1.LocalReport.DataSources = AllFamilies.GetAllProjects;
                

                
            }

            /* DataSet Data = new DataSet();
               Data.ReadXml(this.Context.Server.MapPath(ReportPath + "data.xml"));*/

            //if (MSMODE.Checked)
            //{
            //    Report.Render(Reporting.ReportViewer.RenderType.MSReportViewer, ReportPath, ReportName, ReportName, new ReportDataSource("Sales", Data.Tables[0]));
            //    MessageBox.Show("MS ReportViewer 2010 VWG support is broken -> IFrame Bug, Scriptmanager");
            //}
            //else
            //{
            //  Report.Render(Reporting.ReportViewer.RenderType.PDFReportViewer, ReportPath, ReportName, ReportName, new ReportDataSource("Sales", Data.Tables[0]));
            //}

            // this.Report..LocalReport.DataSources.Clear();
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetMoslemFamily", ListFamilyMoslemproj));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetFoodBox", ListFoodBoxmproj));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetPerson", ListPerson));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetHomeFix", ListHomeFix));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetSchoolBags", ListSchoolBags));

            //this.reportViewer1.Reset();

            //this.reportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
            // ReportDataSource rds = new ReportDataSource("DataSet1",AllFamilies.GetAllProjects);
            // this.reportViewer1.LocalReport.DataSources = AllFamilies.GetAllProjects;

            //this.reportViewer1.DataBind(); 
            // reportmoslemFamilyProjectBindingSource.DataSource = AllFamilies.GetAllProjects; 
            // Below code demonstrate the Parameter passing method. Use only if you have parameters into the reports.


            //ReportParameter p1 = new ReportParameter("FullName", currentPerson.FirstName + " " + currentPerson.FatherName + " " + currentPerson.LastName);
            //ReportParameter p2 = new ReportParameter("PersonID", currentPerson.IDnum);

            //reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1, p2 });
            //reportViewer1.Visible = true;

        }
        private void queryDynamic(ref IQueryable<Tank_Stocks> query, string sizea, string companyname ,string  tanknumber)
        {
            if (!string.IsNullOrEmpty(companyname))
            {
                query = query.Where(b => b.GazCompanyName == companyname);

            }
            if (!string.IsNullOrEmpty(tanknumber))
            {
                query = query.Where(b => b.TankNumber == tanknumber);

            }
            if (!string.IsNullOrEmpty(sizea))
            {
                query = query.Where(b => b.Tank_Size == sizea);

            }
        }
        public void renderrep(int tankid, string sizea, string companyname,string tanknumber)
        {
            using (GazOnDevEntities db = new GazOnDevEntities())
            {

                string tankstatus = tankid.ToString();
                var query1 = from i in db.Tank_Stocks
                             where i.TankSystemStatus == tankstatus
                             select i;
                queryDynamic(ref query1, sizea, companyname, tanknumber);



                //  dd.FirstOrDefault().Datetime = Convert.ToDateTime(dd.FirstOrDefault().Datetime.Value.ToString("MM/dd/yyyy"));
                //dd.FirstOrDefault().Date_of_Fix = Convert.ToDateTime(dd.FirstOrDefault().Date_of_Fix.Value.ToString("MM/dd/yyyy"));

                List<Tank_Stocks> ds2 = query1.ToList<Tank_Stocks>();
                List<Tank_Stocks> ds3 = new List<Tank_Stocks>();
                if (tankstatus == "3" || tankstatus == "4" || tankstatus == "1")
                {
                    foreach (var item in ds2)
                    {
                        string tankTitle = item.TankNumber + "  מספר שיפוץ  " + item.TankFixNumber + "";

                        if (tankstatus == "4")
                        {


                            Invoice inv = db.Invoices.FirstOrDefault(x => x.tankNumber == item.TankNumber);
                            if (inv != null)
                            {
                                string tankTitle2 = item.Address_Tank_From + "\n -------------------- יעד " + inv.address + "";
                                DateTime tempdat =(DateTime) inv.date;
                                string datedd = string.Format("{0}", tempdat.ToString("dd/MM/yyyy"));
                                tankTitle2 += "\n ------------- תאריך יציאה " + datedd + "";  
                                item.Address_Tank_From = tankTitle2;
                            }


                        }

                        if (tankstatus == "4" || tankstatus == "1")
                        {
                            TankFixOperation fixx = db.TankFixOperations.FirstOrDefault(x => x.tankID == item.Id && x.title == "ביצוע בדיקת אטימות");
                            if (fixx != null)
                            {
                                string datett = string.Empty;
                                if (null != fixx.date)
                                {
                                     datett = ((DateTime)fixx.date).ToString("dd/MM/yyyy");
                                }
                              
                               
                                string tankTitle3 = item.Datetime + "\n ------------- תאריך שיפוץ " + datett + "";
                           
                                item.Datetime = tankTitle3;
                            }
                        }

                        item.TankNumber = tankTitle;
                        ds3.Add(item);
                    }
                    ds2 = ds3;
                }


                //  int personSigned = Convert.ToInt32(query1.FirstOrDefault().PersonRecipetsignid);
                // int transportCompanyid = Convert.ToInt32(query1.FirstOrDefault().TransportCompanyID);
                this.reportViewer1.LocalReport.DataSources.Clear();
                // this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dd));
                this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", ds2));
                //     SignPerson personrecipet=   db.SignPersons.FirstOrDefault(x=>x.id == personSigned);
                //  TransportCompany companytrans=   db.TransportCompanies.FirstOrDefault(x=>x.id == transportCompanyid);
                // ReportDataSource rds = new ReportDataSource("DataSet1", dd);
                //  this.reportViewer1.LocalReport.DataSources = dd;



                string tanktype = string.Empty;
                if (tankstatus == "1")
                {
                    tanktype = "במלאי";
                }
                if (tankstatus == "2")
                {
                    tanktype = "בהמתנה";
                }
                if (tankstatus == "3")
                {
                    tanktype = "בשיפוץ";
                }
                if (tankstatus == "4")
                {
                    tanktype = "שיצאו בתעודת משלוח";



                }
                if (tankstatus == "5")
                {
                    tanktype = "פסולים";
                }
                if (tankstatus == "6")
                {
                    tanktype = "מושמדים";
                }



                ReportParameter p1 = new ReportParameter("TankType", tanktype);



                reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1 });

                this.reportViewer1.DataBind();
                reportViewer1.LocalReport.Refresh();
                //  this.reportViewer1.Reset();

                //     this.reportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
                //ReportDataSource rds = new ReportDataSource("DataSet1",AllFamilies.GetAllProjects);
                //this.reportViewer1.LocalReport.DataSources = AllFamilies.GetAllProjects;



            }

            /* DataSet Data = new DataSet();
               Data.ReadXml(this.Context.Server.MapPath(ReportPath + "data.xml"));*/

            //if (MSMODE.Checked)
            //{
            //    Report.Render(Reporting.ReportViewer.RenderType.MSReportViewer, ReportPath, ReportName, ReportName, new ReportDataSource("Sales", Data.Tables[0]));
            //    MessageBox.Show("MS ReportViewer 2010 VWG support is broken -> IFrame Bug, Scriptmanager");
            //}
            //else
            //{
            //  Report.Render(Reporting.ReportViewer.RenderType.PDFReportViewer, ReportPath, ReportName, ReportName, new ReportDataSource("Sales", Data.Tables[0]));
            //}

            // this.Report..LocalReport.DataSources.Clear();
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetMoslemFamily", ListFamilyMoslemproj));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetFoodBox", ListFoodBoxmproj));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetPerson", ListPerson));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetHomeFix", ListHomeFix));
            //this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSetSchoolBags", ListSchoolBags));

            //this.reportViewer1.Reset();

            //this.reportViewer1.LocalReport.ReportPath = Server.MapPath("Report.rdlc");
            // ReportDataSource rds = new ReportDataSource("DataSet1",AllFamilies.GetAllProjects);
            // this.reportViewer1.LocalReport.DataSources = AllFamilies.GetAllProjects;

            //this.reportViewer1.DataBind(); 
            // reportmoslemFamilyProjectBindingSource.DataSource = AllFamilies.GetAllProjects; 
            // Below code demonstrate the Parameter passing method. Use only if you have parameters into the reports.


            //ReportParameter p1 = new ReportParameter("FullName", currentPerson.FirstName + " " + currentPerson.FatherName + " " + currentPerson.LastName);
            //ReportParameter p2 = new ReportParameter("PersonID", currentPerson.IDnum);

            //reportViewer1.LocalReport.SetParameters(new ReportParameter[] { p1, p2 });
            //reportViewer1.Visible = true;

        }
    }
}